package com.wrial.offer;
/*
 * @Author  Wrial
 * @Date Created in 10:02 2020/4/2
 * @Description 和为s的两个数字

输入：nums = [2,7,11,15], target = 9
输出：[2,7] 或者 [7,2]
 */

public class FaceTest_57 {
    // 1.双指针   2.hashmap   3.二分（因为有序）
    // 双指针 一个最前 一个最后如果相加大于target就右指针前移 小于target就左指针后移
    public int[] twoSum(int[] nums, int target) {
        if(nums == null) return new int[2];
        int pLeft = 0;
        int pRight = nums.length-1;
        int[] ans  = new int[2];
        while(pLeft<pRight){

            if(nums[pLeft]+nums[pRight]<target){
                pLeft++;
            }else if(nums[pLeft]+nums[pRight]>target){
                pRight--;
            }else{
                ans[0] = nums[pLeft];
                ans[1] = nums[pRight];
                break;
            }
        }

        return ans;
    }

}
